package com.sxbbc.common.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxbbc.common.core.entity.jz.SysLogs;
import com.sxbbc.common.core.mapper.jz.SysLogsMapper;
import com.sxbbc.common.core.service.ISysLogsService;
import com.sxbbc.common.core.utils.BasePage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 平台操作日志 服务实现类
 * </p>
 *
 * @author leehao
 * @since 2019-04-28
 */
@Service
public class SysLogsServiceImpl extends ServiceImpl<SysLogsMapper, SysLogs> implements ISysLogsService {

    @Override
    public Page<SysLogs> queryLogsList(BasePage<SysLogs> page, SysLogs log) {
        QueryWrapper<SysLogs> wrapper = queryWrapper(log);
        if (page.isPaging()) {
            super.page(page, wrapper);
        } else {
            List<SysLogs> list = super.list(wrapper);
            page.setRecords(list);
        }
        return page;
    }

    /**
     * 日志查询条件
     * @param log
     * @author leehao
     * @return com.baomidou.mybatisplus.mapper.EntityWrapper<com.sxbbc.common.core.entity.jz.SysLogs>
     */
    private QueryWrapper<SysLogs> queryWrapper(SysLogs log) {
        QueryWrapper<SysLogs> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("createtime");
        if (StringUtils.isNotBlank(log.getQueryTime())) {
            String[] times = log.getQueryTime().split("~");
            wrapper.apply("date_format(createtime,'%Y-%m-%d') >= {0}",times[0].trim());
            wrapper.apply("date_format(createtime,'%Y-%m-%d') <= {0}",times[1].trim());
        }
        return wrapper;
    }
}
